package com.lemon.smartsafety.ui.fragment.notice;

import android.os.Bundle;
import android.view.View;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.entity.event.notice.NoticeAddEvent;
import com.lemon.smartsafety.entity.event.notice.NoticeRefreshEvent;
import com.lemon.smartsafety.entity.notice.NoticeInfoVo;
import com.lemon.smartsafety.http.service.NoticeServices;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.ui.activity.notice.NoticeDetailActivity;
import com.lemon.smartsafety.ui.adapter.notice.NoticeAdapter;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafety.view.list.MultiStateView;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.OnClick;
import kale.adapter.LemonRcvAdapter;

/**
 * Created by Cooliean on 16/4/8.
 */
public class NoticeListFragment extends BasePullToRefreshFragment<NoticeInfoVo, List<NoticeInfoVo>> implements RecyclerTClickListener<NoticeInfoVo> {
    private String sHasRead = "";
    private String mKeyWord = "";



    public static NoticeListFragment newInstance() {
        NoticeListFragment pageFragment = new NoticeListFragment();
        return pageFragment;
    }

    public static NoticeListFragment newInstance(String text, String hasRead) {
        Bundle bundle = new Bundle();
        bundle.putString(BundleKey.KEY_WORD, StringUtils.nullValue(text));
        bundle.putString(BundleKey.NOTICE_HAS_READ, StringUtils.nullValue(hasRead));
        NoticeListFragment pageFragment = new NoticeListFragment();
        pageFragment.setArguments(bundle);
        return pageFragment;
    }


    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();

        if (getArguments() != null) {
            mKeyWord = getArguments().getString(BundleKey.KEY_WORD);
            sHasRead = getArguments().getString(BundleKey.NOTICE_HAS_READ);
        }
        sHasRead = StringUtils.nullValue(sHasRead);
        mKeyWord = StringUtils.nullValue(mKeyWord);
    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<NoticeInfoVo> getLemonAdapter() {
        return new NoticeAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return EventTag.TAG_NOTICE_LIST + sHasRead;
    }

    @Override
    public void onLoadMore() {
        NoticeServices.init().getNoticeList(RefreshType.LOAD_MORE, pageSize, currentPage, sHasRead, mKeyWord);
    }

    @Override
    public void onPullRefresh() {
        NoticeServices.init().getNoticeList(RefreshType.REFRESH, pageSize, currentPage, sHasRead, mKeyWord);
    }


    @Override
    public void onItemClick(final NoticeInfoVo data, int position) {
        Bundle bundle = new Bundle();
        bundle.putString(BundleKey.NOTICE_ID, data.getNoticeId());
        bundle.putBoolean(BundleKey.HAS_READ, StringUtils.getBoolean(data.getHasRead()));
        UIHelper.showActivity(getActivity(), bundle, NoticeDetailActivity.class);
    }


    @Override
    protected void initViews() {
        super.initViews();
        //changed by Andrew.yin 2017/10/27 begin  移动端暂时不开放编辑功能
        //if (UserTypeHelper.isGovUser()) {
        //    fab.setVisibility(View.VISIBLE);
        //}else{
            fab.setVisibility(View.GONE);
        //}
        //changed by Andrew.yin 2017/10/27 end
    }

    @OnClick({R.id.float_button})
    public void fabClick(View view) {
        AppService.getBus().post(new NoticeAddEvent());

    }

    @Subscribe
    public void onEventMainThread(NoticeRefreshEvent event) {
        mMultiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
        mPullToRefreshRecyclerView.beginRefreshing();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        OkHttpUtils.getInstance().cancelTag(EventTag.TAG_NOTICE_LIST + sHasRead);
    }
}
